package com.jnu.ilearn;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jnu.ilearn.bean.DataBank;
import com.jnu.ilearn.bean.User;
import com.jnu.ilearn.utils.UserUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChangePassword extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        getSupportActionBar().hide();
        ImageView backView=findViewById(R.id.back);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        EditText password_pastView=findViewById(R.id.password_past);
        EditText password_newView=findViewById(R.id.password_new);
        TextView changePassword=findViewById(R.id.btn_changePassword);
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        DataBank dataBank=new DataBank(ChangePassword.this);
        dataBank.loadData();
        User user =dataBank.getUser();
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() //确认框，点击确定传回数据给主界面
        {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                UserUtils userUtils=new UserUtils();
                Handler handler=new Handler(Looper.getMainLooper()){
                    @Override
                    public void handleMessage(@NonNull Message msg) {
                        super.handleMessage(msg);
                        if(userUtils.isChange()){
                            Toast.makeText(ChangePassword.this,"更改成功",Toast.LENGTH_SHORT);
                            user.setPassword(password_newView.getText().toString());
                            dataBank.saveData();
                            finish();
                        }
                    }
                };
                userUtils.changePassword(handler,user.getAccount(),password_newView.getText().toString());
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        changePassword.setOnClickListener(new View.OnClickListener() {//提交注册按钮响应事件
            @Override
            public void onClick(View v) {
                //将控件与变量绑定，用于获取用户输入的数据
                if(!(password_pastView.getText().toString().equals(user.getPassword()))){
                    new AlertDialog.Builder(ChangePassword.this).setTitle("警告").setMessage("原密码输入错误").show();
                }
                else{
                    builder.setMessage("是否确定更改密码");
                    builder.setTitle("提示");
                    builder.show();
                }
            }
        });
    }
}